package info.qingshui.browser;

import java.net.UnknownHostException;

import java.util.*;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.wjc.httpserver.HttpServerService;

import info.qingshui.browser.R;
import info.qingshui.browser.hk.HKActivity;
import info.qingshui.browser.hk.StartupAsyncTask;
import info.qingshui.browser.hk.setting.Setting;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
public class BrowserApplication extends Application {
	private Setting setting;
	private GoogleAnalyticsTracker tracker;
	
	private static Map<String, Activity> activities = new HashMap<String, Activity>();

	public Setting getSetting() {
		return setting;
	}
	
	public HKActivity getHKActivity() {
		return (HKActivity) activities.get( HKActivity.NAME );
	}
	
	public static Map<String, Activity> getActivities() {
		return activities;
	}



	public void onNewActivity(String name, Activity a) {
		activities.put(name , a);
	}
	
	public void onFinalActivity(String name) {
		activities.remove(name);
//		if( a!= null ) {
//			a.finish();
//		}
	}

	public void onCreate() {
		super.onCreate();
		
	    

		
		//启动代理服务
		setting = Setting.getSetting(this.getBaseContext());
		
        try {
			//HttpServerService.setSocket5Proxy(proxy, port, user, passwd, getApplicationContext(),localport);
        	Intent i = new Intent("info.qingshui.HTTP_SERVICE");
 
        	startService( i );
        } catch (Throwable e) {
			Log.e(BrowserApplication.class.getSimpleName(), "onCreate HttpServerService error",e);
		}
        Log.i(BrowserApplication.class.getSimpleName(), "HttpServerService OK");
		
        setting.setLoginTimes(setting.getLoginTimes() + 1);
        
		// 统计
		tracker = GoogleAnalyticsTracker.getInstance();
		tracker.startNewSession(Setting.GA_UA, 10, this);
		
		
		
//		tracker.trackEvent("LoginLogout", // Category
//				"Login", // Action
//				"common", // Label
//				1); // Value
//		Log.i(BrowserApplication.class.getSimpleName(),
//				"BrowserApplication setting=" + setting);
		
		//

		//第一次使用网路ok才发起
		//new StartupAsyncTask().execute(this);
		
		Log.i(BrowserApplication.class.getSimpleName(),
				"BrowserApplication onCreate");
	}

	public void exit() {// TODO
		//关闭代理服务
		stopService(new Intent("info.qingshui.HTTP_SERVICE"));
		Log.i(BrowserApplication.class.getSimpleName(), "HttpServerService stop");
		getSetting().saveSettingIfNeeded();
//		tracker.trackEvent("LoginLogout", // Category
//				"Logout", // Action
//				"common", // Label
//				1); // Value
		tracker.stopSession();
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0);
		
	}

	public GoogleAnalyticsTracker getTracker() {
		return tracker;
	}

	

}
